package com.yanqu.road.server.manager.activity.shuihuyezipai;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.shuihuyezipai.config.ShuiHuYeZiCardActivityConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.ShuiHuYeZiCardBusiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description 水浒叶子牌配置
 * @Author cwq
 * @Data 2021/12/8 11:11
 */
public class ShuiHuYeZiCardActivityConfigMgr extends TempMgr {

    private static Map<Integer, ShuiHuYeZiCardActivityConfig> configMap = new ConcurrentHashMap<>();

    /**
     * 正在开启的活动配置
     */
    private static Map<Integer, ActivityInfo> openActivityInfoMap = new ConcurrentHashMap<>();

    /**
     * 默认显示的匹配时间
     */
    private static int WAIT_TIME_DEFAULT_VALUE = 30;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        getLogger().info("start reload shuihuyezicard activity config  ");
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.ShuiHuYeZiCardActivity.getValue());

        Map<Integer, ActivityInfo> tempOpenActivityInfoMap = new ConcurrentHashMap<>();
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMap(activityTypeList, nowTimeStr);
        for (ActivityInfo item : activityInfoMap.values()){
            ShuiHuYeZiCardActivityConfig tempShuiHuYeZiCardConfig = new ShuiHuYeZiCardActivityConfig();

            List<Integer> activityIdList = new ArrayList<>();
            activityIdList.add(item.getActivityId());
            Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(item.getActivityId());
            tempShuiHuYeZiCardConfig.setCardConfigMap(ShuiHuYeZiCardBusiness.getShuiHuYeZiCardCardConfigMap(item.getActivityId()));
            tempShuiHuYeZiCardConfig.setBuffConfigMap(ShuiHuYeZiCardBusiness.getShuiHuYeZiCardBuffConfigMap(item.getActivityId()));
            tempShuiHuYeZiCardConfig.setLevelConfigMap(ShuiHuYeZiCardBusiness.getShuiHuYeZiCardLevelConfigMap(item.getActivityId()));
            tempShuiHuYeZiCardConfig.setRoomConfigMap(ShuiHuYeZiCardBusiness.getShuiHuYeZiCardRoomConfigMap(item.getActivityId()));

            ShuiHuYeZiCardActivityConfig.parseConfig(tempShuiHuYeZiCardConfig,activityConfigMap,item);
            configMap.put(item.getActivityId(), tempShuiHuYeZiCardConfig);
            tempOpenActivityInfoMap.put(item.getActivityId(),item);
            getLogger().info("reload shuihuyezicard activity config success. activityId:{}  ",item.getActivityId());
        }
        openActivityInfoMap = tempOpenActivityInfoMap;
        getLogger().info("end reload shuihuyezicard activity config  ");
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static Map<Integer, ShuiHuYeZiCardActivityConfig> getConfigMap() {
        return configMap;
    }

    public static ShuiHuYeZiCardActivityConfig getActivityConfig(int activityId) {
        ShuiHuYeZiCardActivityConfig activityConfig = configMap.get(activityId);
        if(activityConfig == null){
            synchronized (ShuiHuYeZiCardActivityConfigMgr.class){
                if(!configMap.containsKey(activityId)){
                    getLogger().info("水浒叶子牌没有该活动配置。activityId:{},尝试加载活动配置",activityId);
                    try {
                        new ShuiHuYeZiCardActivityConfigMgr().reloadConfig();
                    } catch (Exception e) {
                        getLogger().error("加载 ShuiHuYeZiCardActivityConfig 异常。",e);
                    }
                }
            }
            activityConfig = configMap.get(activityId);
        }
        return activityConfig;
    }


    public static int getWaitTimeDefaultValue() {
        return WAIT_TIME_DEFAULT_VALUE;
    }

    public static Map<Integer, ActivityInfo> getOpenActivityInfoMap() {
        return openActivityInfoMap;
    }
}
